/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path)
{
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str)
{
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url)
{
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str)
{
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str)
{
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str)
{
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email)
{
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str)
{
  if (typeof str === 'string' || str instanceof String)
  {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg)
{
  if (typeof Array.isArray === 'undefined')
  {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}


/**
 * @param {String} P_Password 密码
 * @param {String} P_StrengThRule 强度规则
 * 校验密码强度
 */
export function isCheckPosswordStrength(P_Password, P_StrengThRule, P_NotAllowKey)
{
  console.log(P_StrengThRule)
  //如果强度规则不存在返回通过检验
  if (!P_StrengThRule)
  {
    return {
      isOk: true,
    }
  }
  if (!P_Password)
  {
    return {
      isOk: false,
      msg: "请输入密码"
    }
  }

  try
  {
    //将强度规则字符串转为数组
    let ruleList = JSON.parse(P_StrengThRule);
    let errorStr = "";
    let ipwdLength = 0; //密码长度
    let iLowerCaseCount = 0; //小写字母长度
    let iUpperCaseCount = 0; //大写字母长度
    let iCharCount = 0; //字母长度
    let iNumberCount = 0; //数字长度
    let iSpecicalCount = 0; //特殊符号长度
    let isNotAllowKey = 0; //是否不允许包含登录名或者用户名缩写
    let isNotAllowRepetition = 0;//是否不允许重复字符串
    let isNotOrder = 0;//是否不允许顺序数字和字母
    ruleList.forEach(pre_item =>
    {
      if (pre_item.value)
      {
        if (pre_item.label == "min")
        {
          //密码长度规则
          ipwdLength = Number(pre_item.value);
        }
        else if (pre_item.label == "l")
        {
          iLowerCaseCount = Number(pre_item.value);
        }
        else if (pre_item.label == "u")
        {
          iUpperCaseCount = Number(pre_item.value);
        }
        else if (pre_item.label == "n")
        {
          iNumberCount = Number(pre_item.value);
        }
        else if (pre_item.label == "s")
        {
          iSpecicalCount = Number(pre_item.value);
        }
        else if (pre_item.label == "c")
        {
          iCharCount = Number(pre_item.value);
        } else if (pre_item.label == "k")
        {
          //k 表示关键字 0否 1是
          isNotAllowKey = Number(pre_item.value);
        }
        else if (pre_item.label == "r")
        {
          // r 代表 repetitions
          isNotAllowRepetition = Number(pre_item.value);
        }
        else if(pre_item.label == "o")
        {
          //o 待办 order
          isNotOrder = Number(pre_item.value);
        }
      }
    })

    if (iCharCount > 0 || iLowerCaseCount > 0 || iUpperCaseCount > 0 || iNumberCount > 0 || iSpecicalCount > 0)
    {
      errorStr = errorStr + "密码至少包含";
      if (iCharCount > 0)
      {
        errorStr = errorStr + iCharCount + "位字母,";
      }

      if (iLowerCaseCount > 0)
      {
        errorStr = errorStr + iLowerCaseCount + "位小写字母,";
      }

      if (iUpperCaseCount > 0)
      {
        errorStr = errorStr + iUpperCaseCount + "位大写字母,";
      }

      if (iNumberCount > 0)
      {
        errorStr = errorStr + iNumberCount + "位数字,";
      }

      if (iSpecicalCount > 0)
      {
        errorStr = errorStr + iSpecicalCount + "位特殊字符,";
      }

      if (ipwdLength > 0)
      {
        errorStr += "且";
      }
    }

    if (isNotAllowKey != 0)
    {
      errorStr = errorStr + "不能包含登录名、用户名缩写和手机号,";
    }
    if (isNotAllowRepetition != 0)
    {
      errorStr += "不能有连续相同的字符,"
    }
    if(isNotOrder != 0)
    {
      errorStr += "不能包含"+ isNotOrder +"位及以上的顺序的字母或数字";
    }
    

    if (ipwdLength > 0)
    {
      errorStr += "密码长度不小于" + ipwdLength + "位";
    }
    let valueToReturn = {
      isOk: false,
      msg: errorStr
    };
    console.log(ipwdLength)
    //检验密码是否符号规则
    if (P_Password.length >= ipwdLength)
    {
      let LowerCaseCount = 0; //小写字母长度
      let UpperCaseCount = 0; //大写字母长度
      let CharCount = 0; //字母长度
      let NumberCount = 0; //数字长度
      let SpecicalCount = 0; //特殊符号长度

      for (let i = 0; i < P_Password.length; i++)
      {
        //判断该字符是否为数字
        if ('0' <= P_Password[i] && P_Password[i] <= '9')
        {
          NumberCount++
          //判断是否为小写字母
        } else if ('a' <= P_Password[i] && P_Password[i] <= 'z')
        {
          CharCount++;
          LowerCaseCount++
          //判断是否为大写字母
        } else if ('A' <= P_Password[i] && P_Password[i] <= 'Z')
        {
          CharCount++;
          UpperCaseCount++
          //判断特殊符号
        } else
        {
          SpecicalCount++
        }
      }
      console.log("UpperCaseCount:" + UpperCaseCount)
      console.log("SpecicalCount:" + SpecicalCount)
      if (LowerCaseCount >= iLowerCaseCount && UpperCaseCount >= iUpperCaseCount && CharCount >= iCharCount && NumberCount >= iNumberCount && SpecicalCount >= iSpecicalCount)
      {
        valueToReturn.isOk = true;
      }

      if (valueToReturn.isOk)
      {
        //判断是否包含不允许的字符串
        if (isNotAllowKey != 0 && P_NotAllowKey && P_NotAllowKey.length > 0)
        {
          for (let j = 0; j < P_NotAllowKey.length; j++)
          {
            let findIndex = P_Password.indexOf(P_NotAllowKey[j]);
            if (findIndex >= 0)
            {
              valueToReturn.isOk = false;
              break;
            }
          }
        }
      }
      if (valueToReturn.isOk)
      {
        if (isNotAllowRepetition != 0)
        {
          valueToReturn.isOk = !hasConsecutiveChars(P_Password)
        }
      }
      if(valueToReturn.isOk)
      {
        if(isNotOrder != 0)
        {
          valueToReturn.isOk = !hasOrderChars(P_Password,isNotOrder)
        }
      }
    }
    return valueToReturn;
  }
  catch (e)
  {
    return {
      isOk: true,
      msg: ""
    };
  }
}

function hasConsecutiveChars(str)
{
  return /(.)\1/.test(str); // \1 引用第一个捕获组的内容  
}

/*
 * 用循环遍历字符串 检查每个字符是否与其后一个字符 顺序
 *  P_OrderNum 数量限制 ABC 3个顺序 1234 4个顺序
 */
function hasOrderChars(P_Password, P_OrderNum)
{
  let valueToReturn = false;
  if (P_Password)
  {
    //上一个数的 Unicode
    let previousUnicode = P_Password.charCodeAt(0);
    //出现连续数
    let currentOrderNum = 1;

    //遍历
    for (let i = 0; i < P_Password.length; i++)
    {
      let pre_Unicode = P_Password.charCodeAt(i);
      if (pre_Unicode == previousUnicode + 1)
      {
        currentOrderNum++;
        if (currentOrderNum >= P_OrderNum)
        {
          valueToReturn = true;
          break;
        }
      }
      else
      {
        currentOrderNum = 1;
      }
      previousUnicode = pre_Unicode;
    }
  }
  return valueToReturn;
}
